#define _CRT_SECURE_NO_WARNINGS 1
#include"NewQueue.h"

void QueueInit(QueueList* pq)
{
	assert(pq);
	pq->head = NULL;
	pq->tail = NULL;
}
void QueueDestroy(QueueList* pq)
{
	assert(pq);
	while (pq->head != pq->tail)
	{
		Node* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
	free(pq->head);
	pq->head = pq->tail = NULL;
}
Node* BuyNewNode(QueueList* pq,QueueNodeType x)
{
	Node* newnode = (Node*)malloc(sizeof(Node));
	if (newnode == NULL)
	{
		perror("newnode malloc fail");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}
void QueuePush(QueueList* pq,QueueNodeType x)
{
	assert(pq);
	Node* newnode = BuyNewNode(pq,x);
	if (pq->tail == NULL)
	{
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
}
QueueNodeType QueueTail(QueueList* pq)
{
	assert(pq);
	assert(pq->tail);
	return pq->tail->data;

}

bool QueueEmpty(QueueList* pq)
{
	assert(pq);
	return pq->head == NULL;
}

void QueuePop(QueueList* pq)
{
	assert(pq);
	assert(pq->head);

	if (pq->head == pq->tail)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		Node* cur = pq->head->next;
		free(pq->head);
		pq->head = cur;
	}
}
QueueNodeType QueueFront(QueueList* pq)
{
	assert(pq);
	assert(pq->head);
	return pq->head->data;
}


int QueueSize(QueueList* pq)
{
	int ret = 0;
	assert(pq);
	Node * cur = pq->head;
	while (cur)
	{
		cur = cur->next;
		ret++;
	}
	return ret;
}